#ifndef __MONSTERMANAGER_H__
#define __MONSTERMANAGER_H__


//Controls monsters actions

#include <PA9.h>       // Include for PA_Lib
#include "Init.h"
#include "DisplayText.h"
#include "EnemyFonts.h"
#include "Avatar.h"
#include "PlayerFonts.h"





void MonMan()
{
	//int i = monSprStart;
	int n = 0;
	for(int i = monSprStart; i < (monSprStart + monSize); i++)
	//for(int n = 0; n < + monSize; n++)
	{
		//refresh everything
		monX[n] = bg[n]->getx(); //mates global variable with class
		monY[n] = bg[n]->gety();
		bg[n]->updatePosition(i, oldX, oldY, scrollOffsetX, scrollOffsetY, x, y);
		
		if(bg[n]->invisible() == true)
			{
				PA_StopSpriteAnim(0, i);
				PA_SetSpriteAnim(0, i, 0); //set invisible
				PA_SetSpriteX(0, i, 300);
				PA_SetSpriteY(0, i, 200);
			}
		
		
		//keep looping if not dead
		if (bg[n]->checkdead())
		{	
			//set horizontal flip for animations based on avatars location
			if((PA_GetSpriteX(0, avSprNo) + PA_GetSpriteLx(0, avSprNo)) <= (PA_GetSpriteX(0, i) + PA_GetSpriteLx(0, i)))
			{
				PA_SetSpriteHflip(0, i, 0);
			}
			else
			{
				PA_SetSpriteHflip(0, i, 1); //flip other direction
			}
		
			// if monster is hit by stylus (shot)
			if ((PA_SpriteTouched(i) && Stylus.Newpress)
				|| (PA_SpriteTouched(i) && (monKick == true)))// if monster is hit by stylus (shot)
			{
				
				u16 dmg = 10;
				dmg += rand()%4; //add variancea
				bg[n]->gethurt(dmg); // sends damage to monster
				
				bg[n]->setHitFlag(); //set hit flag
				
				setMonFont(dmg, i, n);
				
				PA_StartSpriteAnimEx(0, i, 16, 21, 42, 1, 1);
				
			}
			//if not hurt
			//if((PA_GetSpriteAnimFrame(0, i) < 16) || (PA_GetSpriteAnimFrame(0, i) > 21))
			if(!(bg[n]->getHitFlag()))
			{
				// === Aggro finding === //
				if (bg[n]->aggro())
				{
					if(bg[n]->getRet() == true)
					{
						bg[n]->retreat(35);// retreat for 35 frames
					}
					else if(bg[n]->attackRange() && bg[n]->getRet() == false) // if in attack range and not retreating
					{
						
						u16 pDmg = 5;
						pDmg += rand()%5; //add variance
						bg[n]->attack(i);
						
						//only execute damage on bite animation
						if(PA_GetSpriteAnimFrame(0, i) == 31)//if(bg[n]->attack(i))
						{
							//if on right side and not guarding
							if((PA_GetSpriteX(0, avSprNo) + PA_GetSpriteLx(0, avSprNo)) <= (PA_GetSpriteX(0, i) + PA_GetSpriteLx(0, i))
							&& (guardR == false))
							{
								setAviFont(pDmg); //set avatar font
							
								if(player.receiveDamage(pDmg)) //if player dead
								{
									//game over
								}
							}
							else if (guardL == false) //if on left and not guarding
							{
								setAviFont(pDmg);
							
								if(player.receiveDamage(pDmg)) //if player dead
								{
									//game over
								}
							}
						}
					}
					else if (bg[n]->getRet() == false) // if not in attack range and not retreating
					{
						bg[n]->pursue(); 
					}
				}
				else 
					bg[n]->wander(i);//wander if no aggro and not dead
			}
		}
		else if (!(bg[n]->checkdead()))// if dead, only do this:
		{
		   //PA_StopSpriteAnim(0,i);
		   if(bg[n]->getAward() == false)
			{
				PA_SetSpriteAnim(0, i, 28);
				player.defeatEnemy(20, 5);
				bg[n]->giveAward();
			}
			//PA_StartSpriteAnimEx(0, i, 22, 28, 7, 1, 1);
			
			//if dead and out of range, hide
			if(bg[n]->invisible())
			{
				PA_SetSpriteAnim(0, i, 0);
				PA_SetSpriteXY(0, i, 300, 200);
			}
		}
			
		//update fonts
		updateMonFont(i, bg[n]->checkdead(), n);
		updateAviFont(player.receiveDamage(0)); 
		
		//if(PA_GetSpriteAnimFrame(0, i) == 21)
			//bg[n]->unsetHitFlag();
		
		n++;
	}
}




#endif  //__MONSTERMANAGER_H__

